﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="@ST.GetStaticFile("My97DatePicker/skin/WdatePicker.css")" rel="stylesheet" />
<script src="@ST.GetStaticFile("My97DatePicker/WdatePicker.js")"></script>

<link href="@ST.GetStaticFile("Models/css/colorbox.css")" rel="stylesheet" />
<link href="@ST.GetStaticFile("Models/css/tree_themes/SimpleTree.css")" rel="stylesheet" />
<script src="@ST.GetStaticFile("Scripts/SimpleTree.js")"></script>
<script src="@ST.GetStaticFile("Scripts/jquery.colorbox.js")"></script>
<div class="right">
    <input type="text" id="hidid" style="display: none">
    <h3>关键字回复管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_register">
                <p><span>关键字：</span><input type="text" id="keyword" placeholder="输入，可添加多个关键字，每个关键字少于30个字符"></p>
                <p>
                    <span>关键字有效时间：</span><input id="validStartTime" type="text" class="manage_register_text02" onfocus="WdatePicker()">
                    <span class="manage_register_font">至</span><input id="validEndTime" type="text" class="manage_register_text02" onfocus="WdatePicker()">
                    <span class="manage_register_font02">
                        <input type="checkbox" id="islongvalid" checked="checked">长期</span>
                </p>
            </div>
            @* <div class="manage_register">
                <div class="manage_register_p">
                    
                    <p>
                        <span><i class="icon iconfont">&#xe602;</i>文本模式</span>
                        <span class="gray"><i class="icon iconfont">&#xe603;</i>图文模式</span>
                        <span>还可以输入<b> 600</b> 字</span>
                    </p>
                    <span>回复内容：</span><textarea class="manage_register_reply" style="height: 100px" id="content"></textarea></div>
            </div>*@


            <div class="manage_register">
                <div class="manage_register_p03">
                    <p>
                        <span><i class="icon iconfont">&#xe602;</i>文本模式</span>
                        <span class="gray" id="divtwxx"><i class="icon iconfont">&#xe603;</i>图文模式</span>
                        <span></span>
                    </p>
                    <textarea class="manage_register_mass" id="content"></textarea>
                </div>
            </div>


            <div class="manage_bottom">
                <table border="0" cellpadding="0" cellspacing="0" width="100%" style="display: none" id="tableNewsReport">
                    <tr>
                        <th><span>编号</span></th>
                        <th><span>标题</span></th>
                        <th><span>栏目</span></th>
                        <th><span>发布时间</span></th>
                    </tr>
                    <tbody id="reportList">
                    </tbody>
                </table>
            </div>

            <div class="manage_register">
                <p>
                    <span>是否有效：</span><input type="radio" id="isvalidYes" name="valid" checked="checked"><span class="manage_register_font">是</span>
                    <input type="radio" id="isvalidNo" name="valid"><span class="manage_register_font">否</span>
                </p>
            </div>
            <p class="manage_add">
                <a id="btnSave" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3, ViewBag.leftid))'>确认</a>
                <a class="manage_add_font" id="btnCancel">取消</a>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>关键词</span></th>
                    <th><span>回复</span></th>
                    <th><span>是否有效</span></th>
                    <th><span>有效开始时间</span></th>
                    <th><span>有效结束时间</span></th>
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    var newsid = "";
    var arrTitles = "";
    var arrMenuNames = "";
    var arrUpdateTimes = "";
    $(document).ready(function () {
        createMemu();
        $("#validStartTime").val(getFormatDate(new Date(), "yyyy-mm-dd", 0));
        $("#validEndTime").val("2099-09-09");
        //保存
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var keyword = $("#keyword").val();
                var content = $("#content").val();
                var validStartTime = $("#validStartTime").val();
                var validEndTime = $("#validEndTime").val();
                var isvalid = $("#isvalidNo").attr("checked") == "checked" ? "0" : "1";
                var islongvalid = $("#islongvalid").attr("checked") == "checked" ? "1" : "0";
                if (id > 0)
                    operate("2", id, keyword, content, validStartTime, validEndTime, isvalid, islongvalid, newsid);
                else
                    operate("1", "0", keyword, content, validStartTime, validEndTime, isvalid, islongvalid, newsid);
            }
        });
        //取消
        $("#btnCancel").click(function () {
            reseting();
        });
        //选择长期checkbox
        $("#islongvalid").change(function () {
            if ($(this).attr("checked") == "checked") {
                $("#validStartTime").val(getFormatDate(new Date(), "yyyy-mm-dd", 0));
                $("#validEndTime").val("2099-09-09");
            }
            else {
                $("#validStartTime").val("");
                $("#validEndTime").val("");
            }

        })
    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            gotoPage();
            $('#divtwxx').colorbox({ href: "@ST.GetTrendsFile("Report/NewsReportListSelected")?weixinid=" + weixinid, title: false, iframe: true, width: "95%", height: "95%" });
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    reseting();
                    getContentMemu();
                })
                getContentMemu();
            }
        });
    }

    function ischeck() {
        var keyword = $("#keyword").val();
        if (keyword == "") {
            alert("关键字不能空！");
            $("#keyword").focus();
            return false;
        }
        if (newsid == "") {
            var content = $("#content").val();
            if (content == "") {
                alert("内容不能空，请重新输入！");
                $("#content").focus();
                return false;
            }
        }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["index"] = pageIndex;
        ajaxdata["size"] = pageSize;
        ajaxdata["weixinid"] = weixinid;
        $.ajax({
            url: "@ST.GetTrendsFile("KeyWordManager/GetKeyWordList")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            dataType: "json",
            success: function (result) {
                if (result != "" && result != null && result != undefined) {
                    //  var arrData = result.split("~");
                    totalCount = result.total;
                    var d = result.dataList;
                    var html = "";
                    for (var i = 0; i < d.length; i++) {
                        html += "<tr>";
                        html += getTxtTd(d[i]["keyword"]) + getTxtTd(d[i]["replyContent"]) + getTxtTd(d[i]["validname"]);
                        //if (d[i]["isLongValid"] != "1" && d[i]["validStartTime"] != null && d[i]["validStartTime"] != "null") {
                        html += getTxtTd(d[i]["validStartTime"].substring(0, 10));
                        //}
                        // else {
                        //   html += getTxtTd("");
                        //}
                        //if (d[i]["isLongValid"] != "1" && d[i]["validEndTime"] != null && d[i]["validEndTime"] != "null") {
                        html += getTxtTd(d[i]["validEndTime"].substring(0, 10));
                        //} else {
                        //    html += getTxtTd("");
                        //}
                        html += "<td><a class=\"manage_bottom_font blue\" onclick=\"getKeyWordByID('" + d[i]["id"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(3, ViewBag.leftid))'>编辑</a>";
                        html += "<a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["id"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(4, ViewBag.leftid))'>删除</a></td>";
                        html += '</tr>';
                    }

                    $("#dvList").html(html);
                    PageCountHtml();
                } else {
                    $("#dvList").html("没有查询到相关数据!");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }
    //查询图文资讯列表
    function getTWList(d) {
        var ids = "";
        var html = "";
        for (var i = 0; i < d.length; i++) {
            html += "<tr>";
            html += getTxtTd(d[i]["ID"]) + getTxtTd(d[i]["title"]) + getTxtTd(d[i]["menuname"]);
            html += getTxtTd(d[i]["updatetime"].substring(0, 10));
            html += '</tr>';
            ids += d[i]["ID"] + '^';
        }
        newsid = ids.substring(0, ids.length - 1);
        $("#reportList").html(html);
    }

    //显示图文资讯列表
    function getTWList2(newsreportid, titles, menunames, updatetimes) {
        var html = "";
        newsreportid = newsreportid.substring(1, newsreportid.length - 1);
        titles = titles.substring(0, titles.length - 1);
        menunames = menunames.substring(0, menunames.length - 1);
        updatetimes = updatetimes.substring(0, updatetimes.length - 1);
        var arryNewsReportIDs = newsreportid.split('^');
        var arryTitles = titles.split('^');
        var arryMenuNames = menunames.split('^');
        var arryUpdateTimes = updatetimes.split('^');
        for (var i = 0; i < arryNewsReportIDs.length; i++) {
            html += "<tr>";
            html += getTxtTd(arryNewsReportIDs[i]) + getTxtTd(arryTitles[i]) + getTxtTd(arryMenuNames[i]);
            html += getTxtTd(arryUpdateTimes[i].substring(0, 10));
            html += '</tr>';
        }
        $("#reportList").html(html);
    }
    //查询
    function getKeyWordByID(id) {
        $.ajax({
            url: "@ST.GetTrendsFile("KeyWordManager/GetKeyWordListByID")",
            type: "GET",
            async: true,
            dataType: "json",
            data: { id: id },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (result) {
              
                var d = result.list1;
               
                var d1 = result.list2;
                $("#hidid").val(id);
                $("#keyword").val(d["keyword"]);
                $("#content").val(d["replyContent"]);
           
                $("#validStartTime").val(d["validStartTime"].substring(0, 10));
           
                $("#validEndTime").val(d["validEndTime"].substring(0, 10));
               
                if (d["isvalid"] == "1")
                    $("#isvalidYes").attr("checked", true);
                else
                    $("#isvalidNo").attr("checked", true);
                if (d["isLongValid"] == "1") {
                    $("#islongvalid").attr("checked", true);
                }
                else {
                    $("#islongvalid").attr("checked", false);
                }

                //显示图文列表
                d1 = eval(d1);
                $("#reportList").html("");
                $("#tableNewsReport").css("display", "none");
                if (d1 != null && d1 != undefined && d1 != "" && d1.length > 0) {
                    $("#tableNewsReport").css("display", "");
                    getTWList(d1);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    //编辑操作
    function operate(op, id, keyword, content, validStartTime, validEndTime, isvalid, islongvalid, newsid) {
        $.ajax({
            url: "@ST.GetTrendsFile("KeyWordManager/UpdateKeyWord")",
            type: "POST",
            async: true,
            data: { op: op, id: id, keyword: keyword, content: content, validStartTime: validStartTime, validEndTime: validEndTime, isvalid: isvalid, islongvalid: islongvalid, weixinid: weixinid, newsreportids: newsid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    reseting();
                    gotoPage();
                    alert("操作成功。");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#keyword").val("");
        $("#content").val("");
        $("#validStartTime").val(getFormatDate(new Date(), "yyyy-mm-dd", 0));
        $("#validEndTime").val("2099-09-09");
        $("#isvalidYes").attr("checked", true);
        $("#islongvalid").attr("checked", true);
        $("#tableNewsReport").css("display", "none");
        $("#reportList").html("");
        newsid = "";
    }

    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }

    function UpdatePD(newsreportids, titles, menunames, updatetimes) {
        $('#divtwxx').colorbox.close();
        newsid = newsreportids;
        if (newsid != "") {
            $("#tableNewsReport").css("display", "");
            //显示图文列表
            getTWList2(newsreportids, titles, menunames, updatetimes);
        }
    }
</script>
